//
//  V_SingleTableViewCell.m
//  FitTime
//
//  Created by dllo on 16/3/31.
//  Copyright © 2016年 赵建平. All rights reserved.
//

#import "V_SingleTableViewCell.h"
#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
#import "Single.h"
#import "CoreDataManager.h"

@interface V_SingleTableViewCell ()<ASIHTTPRequestDelegate>
//@property (nonatomic, retain) ASINetworkQueue *queue;
//@property (nonatomic, strong) UIProgressView *progess;
@property (nonatomic, copy) NSString *urlString;
@property (nonatomic, copy) NSString *topicUrlString;

//@property (nonatomic, strong) CoreDataManager *mangaer;

//提供一个成员属性，接收下载进度值
@property (nonatomic, assign) CGFloat progress;

@property (nonatomic, assign) BOOL isLoaded;

@end


@implementation V_SingleTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
//        self.isLoaded = YES;
        
//        self.mangaer = [CoreDataManager shareCoreDataManager];
        
        self.rightImageView = [UIImageView new];
        [self.contentView addSubview:self.rightImageView];
        
        self.titleLable = [UILabel new];
        [self.contentView addSubview:self.titleLable];
        
        self.difficultLable = [UILabel new];
        [self.contentView addSubview:self.difficultLable];
        
        self.waterView = [UIView new];
        [self.contentView addSubview:self.waterView];
        
        self.toolLable = [UILabel new];
        [self.contentView addSubview:self.toolLable];
        
        self.bodyNameLable = [UILabel new];
        [self.contentView addSubview:self.bodyNameLable];

        self.timeLable = [UILabel new];
        [self.contentView addSubview:self.timeLable];
        /*
        self.downButton = [UIButton new];
        [self.contentView addSubview:self.downButton];
        */
//        self.queue = [[ASINetworkQueue alloc] init];
//        [self.queue reset];
//        [_queue setShowAccurateProgress:YES];
//        [_queue go];
    }
    return self;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    //图片坐标
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView.mas_top).offset(20);
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
        make.width.equalTo(@125);
    }];
    /*
    //下载
    [self.downButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.rightImageView.mas_left).offset(-10);
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.width.mas_equalTo(@20);
        make.height.mas_equalTo(@20);
    }];
    
    [self.downButton setBackgroundImage:[UIImage imageNamed:@"download"] forState:UIControlStateNormal];
    [self.downButton addTarget:self action:@selector(downButtonAction:) forControlEvents:UIControlEventTouchUpInside];
     */
    
    //标题坐标
    [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(20);
        make.left.equalTo(self.contentView.mas_left).offset(20);
        make.height.mas_equalTo(@30);
    }];
    
    //身体部位坐标
    [self.bodyNameLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
        make.left.equalTo(self.contentView.mas_left).offset(20);
        make.height.mas_equalTo(@22.5);
    }];
    
    //难度坐标
    [self.difficultLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bodyNameLable.mas_top).offset(-5);
        make.top.equalTo(self.titleLable.mas_bottom).offset(5);
        make.left.equalTo(self.contentView.mas_left).offset(20);
    }];
    self.difficultLable.text = @"难度";
    //水滴坐标
    [self.waterView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-20);
        make.height.mas_equalTo(@22.5);
        make.left.equalTo(self.bodyNameLable.mas_right).offset(5);
        make.width.mas_equalTo(@45);
    }];
    
    self.waterView.center = CGPointMake(self.difficultLable.center.x / 2, self.difficultLable.center.y / 2);
    
    //工具坐标
    [self.toolLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bodyNameLable.mas_top).offset(-5);
        make.top.equalTo(self.titleLable.mas_bottom).offset(5);
        make.left.equalTo(self.waterView.mas_right).offset(15);
    }];
   

    self.titleLable.font = [UIFont systemFontOfSize:17];
    self.bodyNameLable.font = [UIFont systemFontOfSize:13];
    self.difficultLable.font = [UIFont systemFontOfSize:13];
    self.toolLable.font = [UIFont systemFontOfSize:13];
    
    
    
//    self.rightImageView.backgroundColor = [UIColor redColor];
//    self.titleLable.backgroundColor = [UIColor greenColor];
//    self.toolLable.backgroundColor = [UIColor yellowColor];
//    self.bodyNameLable.backgroundColor = [UIColor orangeColor];
//    self.timeLable.backgroundColor = [UIColor blueColor];
}


- (void)setSingleModel:(SingleModel *)singleModel {
    _singleModel = singleModel;
    //照片
    NSString *photoString = [NSString stringWithFormat:@"http://ft-video.fit-time.cn/%@.png@!medium", singleModel.ID];
    [self.rightImageView sd_setImageWithURL:[NSURL URLWithString:photoString]];
    //标题
    self.titleLable.text = singleModel.title;
    //水滴
    for (int i = 0; i < singleModel.difficulty; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake( 20 * i, 2, 15, 15)];
        imageView.image = [UIImage imageNamed:@"water_b"];
        [self.waterView addSubview:imageView];
    }
    for (int i = (int)singleModel.difficulty; i < 3 ; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake( 20 * i, 2, 15, 15)];
        imageView.image = [UIImage imageNamed:@"water_w"];
        [self.waterView addSubview:imageView];
    }
    
    //器械
    if (singleModel.instrument.length == 0) {
        self.toolLable.text = @"/无器械";
    } else {
        self.toolLable.text = [NSString stringWithFormat:@"/%@", singleModel.instrument];
    }
    //身体部位
    self.bodyNameLable.text = singleModel.part;
    //时间
    NSString *string = [NSString stringWithFormat:@"%.2ld:%.2ld", (long)singleModel.time / 60,(long)singleModel.time % 60];
    NSString *personString = [NSString stringWithFormat:@"%ld", (long)singleModel.playCount];
    //时间坐标
    self.timeLable.text = [NSString stringWithFormat:@"%@   %@人练过",string, personString];
    self.urlString = singleModel.url;
    self.topicUrlString = singleModel.title;
}

- (void)downButtonAction:(UIButton *)downButton {
    
    
    [self.delegate send:self.urlString WithNumber:self.number];
    
//    NSLog(@"%@", self.)
    
//    //Documents 路径
//    NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents"];
//    //下载路径
//    NSString *downLoadPath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4", self.topicUrlString]];
//    NSLog(@"%@", downLoadPath);
//    //下载链接
//    NSURL *url = [NSURL URLWithString:self.urlString];
//    //创建请求
//    ASIHTTPRequest *resquest = [ASIHTTPRequest requestWithURL:url];
//    resquest.delegate = self;
//    
//    
//    
//    if (_isLoaded == YES) {
//        //下载到文件夹
//        [resquest setDownloadDestinationPath:downLoadPath];
//        
//        Single *sing = [NSEntityDescription insertNewObjectForEntityForName:@"Single" inManagedObjectContext:self.mangaer.managedObjectContext];
//        sing.title = self.singleModel.title;
//        sing.singid = self.singleModel.ID;
//        [self.mangaer saveContext];
//        
////        self.isLoaded = NO;
//    } else {
//        NSLog(@"fdgghd0");
//    }
//
//    
//    
//    //缓存路径
//    NSString *tempPath = [path stringByAppendingPathComponent:@"temp"];
//    //缓存文件路径设置
//    [resquest setTemporaryFileDownloadPath:tempPath];
//    //设置断点续传
//    [resquest setAllowResumeForFileDownloads:YES];
//    resquest.downloadProgressDelegate = self.progess;
//    [_queue addOperation:resquest];

}




@end
